//index.js
//获取应用实例
const app = getApp();
var http = require("../../utils/http.js") 

Page({
  data: {
    showLoad: false,
    btnshow: false,
    BBillNo: '',
    BoxNo: [],
  },
  //事件处理函数
  bindViewTap: function() {

  },
  onLoad: function () {
    this.setData({ showLoad: false });
  },
  onShow: function () {
    this.setData({ showLoad: false });
  },
  scanCode(){
    wx.scanCode({
      onlyFromCamera: false,
      success: (res) => {
        console.log(res)
        let args = res.result;
        if(args && args !='') {
          this.setData({ BBillNo: args})
          let query = {
            billno: args
          };
          this.goPost(query);
        } else {
          this.giveError('图像识别失败，请重试')
        }
      }
    })
  },

  giveError(args){
    wx.showToast({
      title: args,
      icon:'none',
      duration: 2000
    })
  },

  changeClass: function () {
    if (this.data.BBillNo) {
      this.setData({
        btnshow: true
      });
    } else {
      this.setData({
        btnshow: false
      });
    }
  },

  getInput(e){
    this.setData({
      BBillNo: (e.detail.value).trim()
    })
    this.changeClass();
  },

  GetBoxRecord(){
    let query = {
          billno : this.data.BBillNo
        };
    this.goPost(query);
  },

  goPost(query){
    this.setData({ showLoad: true });
    http.POST('GetCBoxCodeList', {
      params: query,
      success: (resp) => {
        this.setData({ showLoad: false });
        if (resp.data.Datas.length > 0)
        {
          let boxCodes = ['请选择冷藏箱号'];
          resp.data.Datas.forEach((item) => boxCodes.push(item.CBoxCode))

          if (boxCodes.length == 2) {
            this.setData({ showLoad: true });
            wx.navigateTo({
              url: '/pages/getTempRecord?BBillNo=' + this.data.BBillNo + '&BoxNo=' + boxCodes[1] + ''
            })
          } else {
            this.setData({
              BoxNo: boxCodes
            });
            wx.showActionSheet({
              itemList: this.data.BoxNo,
              itemColor: '#007aff',
              success: (res) => {
                if (res.tapIndex != 0) {
                  this.setData({ showLoad: true });
                  wx.navigateTo({
                    url: '/pages/getTempRecord?BBillNo=' + this.data.BBillNo + '&BoxNo=' + this.data.BoxNo[res.tapIndex] + ''
                  })
                }
              },
              fail: function (res) {
                if (res.errMsg != 'showActionSheet:fail cancel') this.giveError('表单获取失败，请重试')
              }
            })
          }
        }
        else
        {
          this.giveError('没查找到相应的订单信息')
        }
      },
      fail: (err) => {
        this.setData({ showLoad: false });
        //失败后的逻辑  
        wx.showToast({
          icon: 'none',
          title: '请求后台失败',
          duration: 3000,
        })
      },
    });
  }
})
